import java.util.Random;
import java.util.Scanner;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSplitPane;

import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/* VORmain class. The main class of the VOR machine.
 * Uses a text field GUI and generates the output
 * such as deflection, the random radial, if the
 * signal is good or bad, and a text-GUI of the
 * VOR based on deflection
 *
 * Andy Tonthat, Jaimar John Sison, Chris Caoagdan

 */


public class VORmain extends JFrame implements ActionListener{

	  private JPanel contentPane;
      private JTextField textField;
      private TextArea textArea;

	public static void main(String[] args) {
		
		 
			
			   EventQueue.invokeLater(new Runnable() {
                   public void run() {
                           try {
                                   VORmain frame = new VORmain();
                                   frame.setVisible(true);
                           } catch (Exception e) {
                                   e.printStackTrace();
                           }
                   }
           });
			
		}

	
	
	public VORmain(){
	     /**
         * Create the frame.
         */
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 450, 300);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                contentPane.setLayout(new BorderLayout(0, 0));
                setContentPane(contentPane);
                
                textField = new JTextField();
                contentPane.add(textField, BorderLayout.NORTH);
                textField.setColumns(10);
                textField.addActionListener(this);
                
                JButton btnNewButton = new JButton("Submit");
                btnNewButton.addActionListener(this);
                contentPane.add(btnNewButton, BorderLayout.SOUTH);
                
                textArea = new TextArea();
                contentPane.add(textArea, BorderLayout.CENTER);
        }
        
         public void actionPerformed(ActionEvent evt) {
            
                String  text = "";
                int input = -1;
               
               
                try {
                	text = textField.getText();
                	   input = Integer.parseInt(text);
                } catch (NumberFormatException e) {
    
                  
                }
                if(input < 0 || input > 359){
                textArea.append("Please put an input between 0 and 359\n");
                
                }
                
                else{
                	
           		 
            		Radio radio = new Radio();
            		Random random = new Random();
            		//input into the calculations
            		Calculations calc = new Calculations();
            		int randomNum = radio.generateRandomInteger(0, 359, random);
            		int deflection = calc.deflection(randomNum, input);
            		String direction = calc.direction(randomNum, input);
            		String morse = radio.morseCode();

            		int value = radio.OBS();
            		
            		//if the value is zero from the OBS, return bad
            		if(value == 0){
            			textArea.append("Radio signal: BAD\n");
            			textArea.append("Direction: OFF\n");
            			textArea.append("Deflection: " + deflection + "\n\n");
            			
            		}
            		//else return good
            		else{
            			textArea.append("Radio signal: GOOD\n");
            			textArea.append("Radial being sent from radio: " + randomNum + "\n");
            			textArea.append("Deflection: " + deflection + "\n");
            			textArea.append("Direction: " + direction + "\n");
            			textArea.append("Station: " + morse + "\n");
            		
                  //If the plane is 10 or more degrees to the left of radial
            			if(deflection <= -10){
            				textArea.append("|....o.....\n\n");
            				
            			}
                  //If the plane is 9 degrees to the left of radial
            			else if(deflection == -9){
            				textArea.append(".|....o.....\n\n");
            			}
                  //If the plane is 8 degrees to the left of the radial
            			else if(deflection == -8){
            				textArea.append(".|...o.....\n\n");
            				
            			}
                  //If the plane is 7 degrees to the left of the radial   
            			else if(deflection == -7){
            				textArea.append("..|...o.....\n\n");
            			}
                  //if the plane is 6 degrees to the left of the radial   
            			else if(deflection == -6){
            				textArea.append("..|..o.....\n\n");
            			}
                  //if the plane 5 degrees to the left of the radial
            			else if(deflection == -5){
            				textArea.append("...|..o.....\n\n");
            				
            			}
                  //if the plane is 4 degrees to the left of the radial   
            			else if(deflection == -4){
            				textArea.append("...|.o.....\n\n");
            				
            			}
                  //if the plane is 3 degrees to the left of the radial   
            			else if(deflection == -3){
            				textArea.append("....|.o.....\n\n");
            			}
                 //if the plane is 2 degrees to the left of the radial    
            			else if(deflection == -2){
            				textArea.append("....|o.....\n\n");
            				
            			}
                  //if the plane is 1 degree to the left of the radial   
            			else if(deflection == -1){
            				textArea.append(".....|o.....\n\n");
            			}
                  //if the plane is on the radial   
            			else if(deflection == 0){
            				textArea.append(".....|.....\n\n");
            			}
                  //if the plane is 1 degree to the right of the radial
            			else if(deflection == 1){
            				textArea.append(".....o|.....\n\n");
            				
            			}
                  //if the plane is 2 degrees to the right of the radial   
            			else if(deflection == 2){
            				textArea.append(".....o|....\n\n");
            			}
                  //if the plane is 3 degrees to the right of the radial
            			else if(deflection == 3){
            				textArea.append(".....o.|....\n\n");
            			}
                  //if the plane is 4 degrees to the right of the radial
            			else if(deflection == 4){
            				textArea.append(".....o.|....\n\n");
            			
            			}
                  //if the plane is 5 degrees to the right of the radial   
            			else if(deflection == 5){
            				textArea.append(".....o..|...\n\n");
            				
            			}
                  //if the plane is 6 degrees to the right of the radial   
            			else if(deflection == 6){
            				textArea.append(".....o..|..\n\n");
            				
            			}
                  //if the plane is 7 degrees to the right of the radial   
            			else if(deflection == 7){
            				textArea.append(".....o...|..\n\n");
            			}
                  //if the plane is 8 degrees to the right of the radial   
            			else if(deflection == 8){
            				textArea.append(".....o...|.\n\n");
            			}
                  //if the plane is 9 degrees to the right of the radial   
            			else if(deflection == 9){
            				textArea.append(".....o....|.\n\n");
            			
            			}
                  //if the plane is 10 or more degrees to the right of the radial   
            			else if(deflection >= 10){
            				textArea.append(".....o....|\n\n");
            				
            			}
            		}
                textField.selectAll();
               
         }
		
		
	}
         
}
